Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

445
Views
How to direct error > 500 with Axios Interceptor

How to direct error response with Axios Interceptor. I used axios interceptor for all http requests!

the expectation is that when the server error 500, 501 etc, will be redirected to route "/500"

export const request = axios.create({ baseURL: BASE_URL_API });
request.interceptors.request.use(
  async (config: AxiosRequestConfig) => {
    try {
      const token = GetCookie('token');      

      config.headers = {
        Authorization: `Bearer ${token}`,
      };
      return config;
    } catch (errorConfig) {
      return Promise.reject(errorConfig);
    }
  },
  error => {
    return Promise.reject(error);
  },
);

// Add a response interceptor
request.interceptors.response.use(
  response => {
    return response;
  },
  async (error: AxiosError) => { 
    const status = error.response?.status;
    try {
      if (status === 401 && error.response?.data.error.message === "Full authentication is required to access this resource" ) {
        RemoveCookie('token')
        RemoveCookie('_currentUser')
        window.location.href = '/';
      } else {
        return Promise.reject(error);
      }
      return Promise.reject(error);
    } catch (errorValue) {
      return Promise.reject(errorValue);
    }
  },
);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!